package org.bluedream.comm.base;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.*;
import lombok.ToString;
import org.bluedream.comm.utils.EmptyUtil;

import java.util.List;

@ToString(callSuper = true)
@NoArgsConstructor
//@EqualsAndHashCode(callSuper = false)
public class TreeEntity<T extends TreeEntity<?>> extends PageEntity<T> implements Comparable<TreeEntity>{
    private static final long serialVersionUID = 2213295172928493965L;
    //非数据库字段
    @TableField(exist = false)
    protected Boolean isQueryChildren;

    //非数据库字段
    @TableField(exist = false)
    @Setter
    @Getter
    protected T parent;
    @TableField(exist = false)
    @Setter
    @Getter
    protected List<T> childList;
    @TableField(exist = false)
    public static final String ROOT_CODE = "0";     //root节点
    @TableField(exist = false)
    public static final int DEFAULT_TREE_SORT = 30; //默认排序 数值
    @TableField(exist = false)
    public static final int TREE_SORTS_LENGTH = 10; //排序递增值
    @TableField(exist = false)
    public static final String DEFAULT_TREE_CODE = "001"; //默认 tree code
    @Getter
    @Setter
    @NonNull
    protected String treeName;    //节点名称
    @Getter
    @NonNull
    protected String parentCode;    //父级编号
    @Getter
    @NonNull
    protected String parentCodes;   //所有父级编号
    @Setter
    @Getter
    @NonNull
    protected Integer treeSort;
    @Getter
    @NonNull
    protected String treeSorts;
    @Setter
    @Getter
    @NonNull
    protected String treeLeaf; //是否末级
    @Getter
    @NonNull
    protected Integer treeLevel; //层次级别
    @Getter
    @NonNull
    protected String treeNames;  //全节点名
    @Setter
    @Getter
    @NonNull
    protected String treeCode;

    @TableField(exist = false)
    protected boolean isChecked;

    @TableField(exist = false)
    protected boolean isTreeLeaf;

    /**
     * 是否限制层级，true时，需要配合maxTreeLevel，否则报异常
     */
    @TableField(exist = false)
    @Setter
    protected boolean limitTreeLevel;

    @TableField(exist = false)
    @Setter
    @Getter
    protected int maxTreeLevel;

    public TreeEntity(String id) {
        super(id);
    }

    public TreeEntity(long limit , long pageSize) {
        super(limit , pageSize);
    }

    public boolean getIsTreeLeaf() {
        return "1".equals(this.treeLeaf);
    }

    public void setTreeLevel(){
        this.treeLevel = this.parentCodes != null ? this.parentCodes.split(",").length - 1 : 0;
    }

    public boolean getLimitTreeLevel(){
        return this.limitTreeLevel;
    }

    public void setTreeNames(){
        String a = this.ROOT_CODE + "," + getTreeName() + ",";
        if (EmptyUtil.isNoEmpty(this.parent)){
            String parentId = this.parent.getId();
            if (EmptyUtil.isNoEmpty(parentId) && !parentId.equals("0")){
                a = this.parent.getTreeNames() + getTreeName() + ",";
            }
        }
        this.treeNames = a;
    }

    public void setParentCode(){
        if (EmptyUtil.isEmpty(this.parent)){
            this.parentCode = "0";
        }else if (EmptyUtil.isNoEmpty(this.parent) && (EmptyUtil.isEmpty(this.parent.getId()) || this.parent.getId().equals("0"))){
            this.parentCode = "0";
        } else {
            this.parentCode = this.parent.getId();
        }
    }

    public void setParentCodes(){
        String a = this.ROOT_CODE + ",";
        if (EmptyUtil.isNoEmpty(this.parent)) {
            String parentId = this.parent.getId();
            if (EmptyUtil.isNoEmpty(parentId) && !parentId.equals("0")){
                a = this.parent.getParentCodes() + this.parent.getId() + ",";
            }
        }
        this.parentCodes = a;
    }

    public void setTreeSorts(){
        String a = this.treeSort + ",";
        if (EmptyUtil.isNoEmpty(this.parent)){
            String parentId = this.parent.getId();
            if (EmptyUtil.isNoEmpty(parentId) && !parentId.equals("0")){
                a = this.parent.getTreeSorts() + this.treeSort + ",";
            }
        }
        this.treeSorts = a;
    }

    public void setTreeEntityGoal(){
        initParentParameter();
        initTreeParameter();
        setTreeSorts();
    }

    public void initParentParameter(){
        setParentCode();
        setParentCodes();
    }

    public void initTreeParameter(){
        setTreeLevel();
        setTreeNames();
    }

    @Override
    public int compareTo(TreeEntity o) {
        return this.getTreeSort() - o.getTreeSort();
    }
}
